// 定义类-蛇

class Snake {
    snakeElement:HTMLElement = document.getElementById('snake')!
    // 获取蛇的第一个div相当于蛇头
    snakeHead:HTMLElement = document.querySelector('#snake>div')!
    // 获取蛇身
    bodies = this.snakeElement.getElementsByTagName('div')

    get X() {
        return this.snakeHead.offsetLeft
    }
    get Y() {
        return this.snakeHead.offsetTop
    }

    set X(value) {
        // 判断是否撞墙，然后抛出错误
        if(value < 0 || value > 290) {
            throw new Error("您撞墙了，游戏结束");
        }else {
           
            
            this.snakeHead.style.left = value + 'px'
        }
        
    }

    set Y(value) {
        // 判断是否撞墙，然后抛出错误
        if(value < 0 || value > 290) {
            throw new Error("您撞墙了，游戏结束");
        }else {
            this.snakeHead.style.top = value + 'px'
        }

    }

    // 移动身体
    moveBody() {
        for (let index = this.bodies.length-1; index > 0; index--) {
            console.log(this.bodies[index].style.left,this.bodies[index-1].style.left);
            
            this.bodies[index].style.left = this.bodies[index-1].style.left
            this.bodies[index].style.top = this.bodies[index-1].style.top
        }
    }
    



}
export default Snake